layui.use(['form','layer','table','jquery'],function () {
    var $ = layui.jquery;
    var layer = layui.layer;
    var table = layui.table;
    var form = layui.form;

    var userId = localStorage.getItem("userId");
    var trueName = localStorage.getItem("trueName");
    var account = localStorage.getItem("account");
    var userName = localStorage.getItem("userName");

    BeforeCountAll("",$("#span1"));
    BeforeCountAll("1",$("#span2"));
    BeforeCountAll("2",$("#span3"));
    BeforeCountAll("3",$("#span4"));
    function BeforeCountAll(status,name) {
        $.ajax({
            type:"post",
            dataType:"json",
            data:{"status":status,"type":4},
            url:"/MicrocirclComment/selectBeforeCountAll",
            success:function (data) {
                name.text(data.data);
            }
        })
    };
    BeforeCount("");
    //前台表格数据
    function BeforeCount(status) {
        var commentDate = new Date();
        commentDate = getTime(commentDate);
        $.ajax({
            type:"post",
            dataType:"json",
            data:{"type":4,"status":status,"commentDate":commentDate},
            url:"/MicrocirclComment/selectBeforeCountToday",
            success:function (data) {
                var data1 = data.data.goodsCountAllComment;
                var data2 = data.data.countCommentsToday;
                var fillZero1 = isRealNum(data1);
                var fillZero2 = isRealNum(data2);
                $("#title").empty();
                $("#sum").empty();
                $("#title").append(
                    "<th>"+"历史评论次数"+"</th>" +
                    "<th>"+"历史评论人数"+"</th>" +
                    "<th>"+"今日评论次数"+"</th>" +
                    "<th>"+"今日评论人数"+"</th>"
                );

                $("#sum").append(
                    "<th>"+fillZero1+"</th>"
                    +"<th>"+fillZero1+"</th>"
                    +"<th>"+fillZero2+"</th>"
                    +"<th>"+fillZero2+"</th>"
                );
            }
        })
    };
    //全部评论
    $("#quanbu").click(function () {
        show("quanbu");
        end("zhengchang");
        end("yilanjie");
        end("yishanchu");
        var inp = $("#in").val();
        if(inp=="1"){
            selectBeforeTable("/MicrocirclComment/selectBeforeTable?status="+status+"&type="+type)
            BeforeCount("");
        }else{
            selectUserTable("/MicrocirclComment/selectUserTable?status="+status+"&type="+type);
            UserCount("");
        }
    });
    //正常
    $("#zhengchang").click(function () {
        end("quanbu");
        show("zhengchang");
        end("yilanjie");
        end("yishanchu");
        var inp = $("#in").val();
        if(inp=="1"){
            selectBeforeTable("/MicrocirclComment/selectBeforeTable?status="+1+"&type="+type)
            BeforeCount(1);
        }else{
            selectUserTable("/MicrocirclComment/selectUserTable?status="+1+"&type="+type);
            UserCount(1);
        }
    });
    //已拦截
    $("#yilanjie").click(function () {
        end("quanbu");
        end("zhengchang");
        show("yilanjie");
        end("yishanchu");
        var inp = $("#in").val();
        if(inp=="1"){
            selectBeforeTable("/MicrocirclComment/selectBeforeTable?status="+2+"&type="+type)
            BeforeCount(2);
        }else{
            selectUserTable("/MicrocirclComment/selectUserTable?status="+2+"&type="+type);
            UserCount(2);
        }
    });
    //已删除
    $("#yishanchu").click(function () {
        end("quanbu");
        end("zhengchang");
        end("yilanjie");
        show("yishanchu");
        var inp = $("#in").val();
        if(inp=="1"){
            selectBeforeTable("/MicrocirclComment/selectBeforeTable?status="+3+"&type="+type);
            BeforeCount(3);
        }else{
            selectUserTable("/MicrocirclComment/selectUserTable?status="+3+"&type="+type);
            UserCount(3);
        }
    });
    var status = "";
    var type=4;
    selectBeforeTable("/MicrocirclComment/selectBeforeTable?status="+status+"&type="+type);
    function selectBeforeTable(url) {
        table.render({
            elem:"#root",
            url:url,
            defaultToolbar: ['','',''],
            page:true,
            limit:10,
            limits:[10,15,20],
            size: 'lg',
            cols:[[
                {type:"numbers",title:"序号",align:"center"},
                {field:"account",title:"评论人账号",align:"center"},
                {field:"nickname",title:"评论人昵称",align:"center"},
                {field:"artcleTitle",title:"所属微圈",align:"center"},
                {field:"commentDate",title:"发言时间",align:"center"},
                {field:"content",title:"发言内容",align:"center"},
                {field:"status",title:"状态",align:"center",templef:function (data) {
                        if(data.status=="1"){
                            return "正常";
                        }else if(data.status=="2"){
                            return "已拦截"
                        }else{
                            return "已删除";
                        }
                    }},
                {title:"操作",align:"center",templet:function (data) {
                        if(data.status=="1"){
                            return "<div>"
                                +"<a href='javascript:chakan("+data.id+");' style='color: #2F4056;margin-right: 10px;'>查看</a>"
                                +"<a href='javascript:lanjie("+data.id+");' style='color: #2F4056;margin-right: 10px;'>拦截</a>"
                                +"<a href='javascript:shanchu("+data.id+");' style='color: #2F4056;margin-right: 10px;'>删除</a>"
                                +"</div>";
                        }else if(data.status=="2"){
                            return "<div>"
                                + "<a href='javascript:chakan(" + data.id + ");' style='color: #2F4056;margin-right: 10px;'>查看</a>"
                                + "<a href='javascript:shanchu(" + data.id + ");' style='color: #2F4056;margin-right: 10px;'>删除</a>"
                                + "</div>";
                        }else if(data.status=="3"){
                            return "<div>"
                                + "<a href='javascript:chakan(" + data.id + ");' style='color: #2F4056;margin-right: 10px;'>查看</a>"
                                + "</div>";
                        }
                    }}
            ]]
        })
    };

    //查看
    chakan = function chakan(id){
        $.ajax({
            type:"post",
            dataType:"json",
            data:{"id":id,"type":4},
            url:"/MicrocirclComment/selectBeforeJsMicrocircle",
            success:function (data) {
                BeforeLayer(data.data,"/comment/comment-microcircle-see.html");
            }
        });
    };
    function BeforeLayer(data,content){
        var obj;
        layer.open({
            type:2,
            shode:0,
            maxmin:true,
            title:"查看微圈评论",
            content:content,
            area:["800px","600px"],
            btn:["关闭"],
            yes:function(index){
                layer.close(index);
            },success:function (layero,index) {
                obj = layero.find('iframe').contents();
                localStorage.setItem("commodityId", data.commodityId);
                obj.find("#artcleTitle").text(data.artcleTitle);
                obj.find("#artcleKeywords").text(data.artcleKeywords);
                obj.find("#trueName").text(data.trueName);
                obj.find("#createDate").text(data.createDate);
                obj.find("#releaseDate").text(data.releaseDate);
                obj.find("#acticleHits").text(data.acticleHits);
                obj.find("#acticleLike").text(data.acticleLike);
                obj.find("#acticleReviews").text(data.acticleReviews);
                obj.find("#acticleShare").text(data.acticleShare);
                obj.find("#artcleUrl").attr("src",'http://'+data.artcleUrl);
                if(data.show=="2"){
                    var show="显示中";
                    obj.find("#show").text(show);
                }else{
                    var show = "隐藏中";
                    obj.find("#show").text(show);
                }
                obj.find("#article").text(data.article);
                if(data,details=="2"){
                    var details = "草稿";
                    obj.find("#details").text(details);
                }else if(data,details=="3"){
                    var details = "已通过";
                    obj.find("#details").text(details);
                }else if(data,details=="4"){
                    var details = "未提交";
                    obj.find("#details").text(details);
                }else if(data,details=="5"){
                    var details = "已提交";
                    obj.find("#details").text(details);
                }else {
                    var details = "退回";
                    obj.find("#details").text(details);
                }
                obj.find("#userId").val(userId);
                obj.find("#trueNamee").val(userName);
                obj.find("#account").val(account);
            }
        })
    };
    //拦截
    lanjie = function lanjie(id){
        layer.confirm("是否确定拦截这条评论",{
                icon:3,title:"提示"
            },
            function (index) {
                $.ajax({
                    type:"post",
                    dataType:"json",
                    data:{"id":id,"status":2},
                    url:"/comment/delGoodsComment",
                    success:function (data) {
                        if(data){
                            layer.msg("删除成功");
                            location.reload();
                        }else{
                            layer.msg("删除失败");
                        }
                    }
                })
            }
        )
    }
    //删除
    shanchu = function shanchu(id){
        layer.confirm("是否确定删除这条评论",{
                icon:3,title:"提示"
            },
            function (index) {
                $.ajax({
                    type:"post",
                    dataType:"json",
                    data:{"id":id,"status":3},
                    url:"/comment/delGoodsComment",
                    success:function (data) {
                        if(data){
                            layer.msg("删除成功");
                            location.reload();
                        }else{
                            layer.msg("删除失败");
                        }
                    }
                })
            }
        )
    };

    //显示
    function show(elementId) {
        var obj1 = document.getElementById(elementId);
        obj1.style.backgroundColor="#2F4056";
        obj1.style.color="#FFFFFF";
    };
    //不显示
    function end(elementId) {
        var obj2 = document.getElementById(elementId);
        obj2.style.backgroundColor="#F9F9F9";
        obj2.style.color="#666666";
        obj2.style.border="1px solid #D2D2D2";
    };

    //顶部时间
    function getTime(date){
        var year = date.getFullYear(); //获取完整的年份(4位,1970-????)
        var month = date.getMonth()+1; //获取当前月份(0-11,0代表1月)
        var today = date.getDate(); //获取当前日(1-31)
        var hour = date.getHours(); //获取当前小时数(0-23)
        var minute = date.getMinutes(); //获取当前分钟数(0-59)
        var second = date.getSeconds(); //获取当前秒数(0-59)
        var nowTime;
        nowTime = year+'-'+fillZero(month)+'-'+fillZero(today);
        return nowTime;
    };
    //时间的为零操作
    function fillZero(str){
        var realNum;
        if(str<10){
            realNum	= '0'+str;
        }else{
            realNum	= str;
        }
        return realNum;
    };
    function isRealNum(val){
        // isNaN()函数 把空串 空格 以及NUll 按照0来处理 所以先去除
        if(val === "" || val ==null){
            return 0;
        }
        if(!isNaN(val)){
            return val;
        }else{
            return 0;
        }
    };
    //管理人员按钮
    function UserCountAll(status,name) {
        $.ajax({
            type:"post",
            dataType:"json",
            data:{"status":status,"type":4},
            url:"/MicrocirclComment/selectUserCountAll",
            success:function (data) {
                name.text(data.data);
            }
        })
    };

    //管理人员表格数据
    function UserCount(status) {
        var commentDate = new Date();
        commentDate = getTime(commentDate);
        $.ajax({
            type:"post",
            dataType:"json",
            data:{"type":4,"status":status,"commentDate":commentDate},
            url:"/MicrocirclComment/selectUserCountToday",
            success:function (data) {
                var data1 = data.data.goodsCountAllComment;
                var data2 = data.data.countCommentsToday;
                var fillZero1 = isRealNum(data1);
                var fillZero2 = isRealNum(data2);
                $("#title").empty();
                $("#sum").empty();
                $("#title").append(
                    "<th>"+"历史评论次数"+"</th>" +
                    "<th>"+"历史评论人数"+"</th>" +
                    "<th>"+"今日评论次数"+"</th>" +
                    "<th>"+"今日评论人数"+"</th>"
                );

                $("#sum").append(
                    "<th>"+fillZero1+"</th>"
                    +"<th>"+fillZero1+"</th>"
                    +"<th>"+fillZero2+"</th>"
                    +"<th>"+fillZero2+"</th>"
                );
            }
        })
    };

    function selectUserTable(url) {
        table.render({
            elem:"#root",
            url:url,
            defaultToolbar: ['','',''],
            page:true,
            limit:10,
            limits:[10,15,20],
            size: 'lg',
            cols:[[
                {type:"numbers",title:"序号",align:"center"},
                {field:"account",title:"评论人账号",align:"center"},
                {field:"trueName",title:"评论人昵称",align:"center"},
                {field:"artcleTitle",title:"所属微圈",align:"center"},
                {field:"commentDate",title:"发言时间",align:"center"},
                {field:"content",title:"发言内容",align:"center"},
                {field:"status",title:"状态",align:"center",templef:function (data) {
                        if(data.status=="1"){
                            return "正常";
                        }else if(data.status=="2"){
                            return "已拦截"
                        }else{
                            return "已删除";
                        }
                    }},
                {title:"操作",align:"center",templet:function (data) {
                        if(data.status=="1"){
                            return "<div>"
                                +"<a href='javascript:chakan2("+data.id+");' style='color: #2F4056;margin-right: 10px;'>查看</a>"
                                +"<a href='javascript:lanjie("+data.id+");' style='color: #2F4056;margin-right: 10px;'>拦截</a>"
                                +"<a href='javascript:shanchu("+data.id+");' style='color: #2F4056;margin-right: 10px;'>删除</a>"
                                +"</div>";
                        }else if(data.status=="2"){
                            return "<div>"
                                + "<a href='javascript:chakan2(" + data.id + ");' style='color: #2F4056;margin-right: 10px;'>查看</a>"
                                + "<a href='javascript:shanchu(" + data.id + ");' style='color: #2F4056;margin-right: 10px;'>删除</a>"
                                + "</div>";
                        }else if(data.status=="3"){
                            return "<div>"
                                + "<a href='javascript:chakan2(" + data.id + ");' style='color: #2F4056;margin-right: 10px;'>查看</a>"
                                + "</div>";
                        }
                    }}
            ]]
        });
    };
    //查看
    chakan2 = function chakan2(id){
        $.ajax({
            type:"post",
            dataType:"json",
            data:{"id":id,"type":4},
            url:"/MicrocirclComment/selectUserJsMicrocircle",
            success:function (data) {
                UserLayer(data.data,"/comment/comment-microcircle-see2.html");
            }
        });
    };
    function UserLayer(data,content){
        var obj;
        layer.open({
            type:2,
            shode:0,
            maxmin:true,
            title:"查看微圈评论",
            content:content,
            area:["800px","600px"],
            btn:["关闭"],
            yes:function(index){
                layer.close(index);
            },success:function (layero,index) {
                obj = layero.find('iframe').contents();
                localStorage.setItem("commodityId", data.commodityId);
                obj.find("#artcleTitle").text(data.artcleTitle);
                obj.find("#artcleKeywords").text(data.artcleKeywords);
                obj.find("#trueName").text(data.trueName);
                obj.find("#createDate").text(data.createDate);
                obj.find("#releaseDate").text(data.releaseDate);
                obj.find("#acticleHits").text(data.acticleHits);
                obj.find("#acticleLike").text(data.acticleLike);
                obj.find("#acticleReviews").text(data.acticleReviews);
                obj.find("#acticleShare").text(data.acticleShare);
                obj.find("#artcleUrl").attr("src",'http://'+data.artcleUrl);
                if(data.show=="2"){
                    var show="显示中";
                    obj.find("#show").text(show);
                }else{
                    var show = "隐藏中";
                    obj.find("#show").text(show);
                }
                obj.find("#article").text(data.article);
                if(data,details=="2"){
                    var details = "草稿";
                    obj.find("#details").text(details);
                }else if(data,details=="3"){
                    var details = "已通过";
                    obj.find("#details").text(details);
                }else if(data,details=="4"){
                    var details = "未提交";
                    obj.find("#details").text(details);
                }else if(data,details=="5"){
                    var details = "已提交";
                    obj.find("#details").text(details);
                }else {
                    var details = "退回";
                    obj.find("#details").text(details);
                }
                obj.find("#userId").val(userId);
                obj.find("#trueNamee").val(userName);
                obj.find("#account").val(account);
            }
        })
    };
    //点击前端人员
    $("#qianduan").click(function () {
        $("#in").val(1);
        show("qianduan");
        end("guanli");
        show("quanbu");
        end("zhengchang");
        end("yilanjie");
        end("yishanchu");
        BeforeCountAll("",$("#span1"));
        BeforeCountAll("1",$("#span2"));
        BeforeCountAll("2",$("#span3"));
        BeforeCountAll("3",$("#span4"));
        BeforeCount("");
        selectBeforeTable("/MicrocirclComment/selectBeforeTable?status="+status+"&type="+type);
    });
    //点击管理人员
    $("#guanli").click(function () {
        $("#in").val(2);
        show("guanli");
        end("qianduan");
        show("quanbu");
        end("zhengchang");
        end("yilanjie");
        end("yishanchu");
        UserCountAll("",$("#span1"));
        UserCountAll("1",$("#span2"));
        UserCountAll("2",$("#span3"));
        UserCountAll("3",$("#span4"));
        UserCount("");
        selectUserTable("/MicrocirclComment/selectUserTable?status="+status+"&type="+type);
    });

    //刷新
    $('#shuaxin').click(function () {
        location.reload();
    });

    //返回
    $("#goBack").click(function (s) {
        parent.$('#Jump').val('首页');
        parent.getQuick();
        return;
    });
});